package cn.ttxs.lib.base;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.vondear.rxtool.view.RxToast;

import cn.ttxs.lib.base.ibase.IBaseFragment;
import cn.ttxs.lib.tool.ToolLog;


/**
 * Created by bsh_kt on 2017/6/8.
 */
public abstract class BaseFragment extends Fragment implements IBaseFragment {
    public static final String ARGS_PAGE = "args_page";

    /**
     * 当前Fragment渲染的视图View
     **/
    private View mContextView = null;
    protected boolean isVisible = false;

    /**
     * 日志输出标志
     **/
    protected final String TAG = this.getClass().getSimpleName();

    /**
     * 网络访问时发起的loading
     */
    protected Dialog dialog;


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ToolLog.d(TAG, "BaseFragment-->onAttach()");
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ToolLog.d(TAG, "BaseFragment-->onCreate()");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        ToolLog.d(TAG, "BaseFragment-->onCreateView()");
        //渲染视图View(防止切换时重绘View)
        if (null != mContextView) {
            ViewGroup parent = (ViewGroup) mContextView.getParent();
            if (null != parent) {
                parent.removeView(mContextView);
            }
        } else {
            mContextView = inflater.inflate(bindLayout(), container, false);
            // 控件初始化
            initView(mContextView, savedInstanceState);
        }
        //业务处理
        doBusiness(getActivity());
        return mContextView;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            isVisible = true;
            lazyLoad();
        } else {
            isVisible = false;
        }
    }


    /**
     * 懒加载
     */
    protected void lazyLoad() {
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        ToolLog.d(TAG, "BaseFragment-->onActivityCreated()");
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        ToolLog.d(TAG, "BaseFragment-->onSaveInstanceState()");
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onStart() {
        ToolLog.d(TAG, "BaseFragment-->onStart()");
        super.onStart();
    }

    @Override
    public void onResume() {
        ToolLog.d(TAG, "BaseFragment-->onResume()");
        super.onResume();
    }

    @Override
    public void onPause() {
        ToolLog.d(TAG, "BaseFragment-->onPause()");
        super.onPause();
    }

    @Override
    public void onStop() {
        ToolLog.d(TAG, "BaseFragment-->onStop()");
        super.onStop();
        dismissLoading();
    }

    @Override
    public void onDestroy() {
        ToolLog.d(TAG, "BaseFragment-->onDestroy()");
        super.onDestroy();
    }

    @Override
    public void onDetach() {
        ToolLog.d(TAG, "BaseFragment-->onDetach()");
        super.onDetach();
    }

    /**
     * 获取当前Fragment依附在的Activity
     *
     * @return
     */
    @Override
    public Activity getContext() {
        return getActivity();
    }

    @Override
    public int bindLayout() {
        return 0;
    }

    @Override
    public void initView(View view, Bundle bundle) {

    }

    @Override
    public void doBusiness(Context mContext) {

    }
//
//    public void showLoading(Activity activity, boolean canCancel) {
//        this.dismissLoading();
//        dialog = new CusLoading(getContext(), R.style.CustomDialog, canCancel);
//        showDialog(activity, dialog);
//    }
//
//    public void showLoading(Activity activity) {
//        this.dismissLoading();
//        dialog = new CusLoading(getContext(), R.style.CustomDialog, true);
//        showDialog(activity, dialog);
//    }
//
//    public void showLoading(Activity activity, String text, boolean canCancel) {
//        this.dismissLoading();
//        dialog = new CusLoading(getContext(), R.style.CustomDialog, text, canCancel);
//        showDialog(activity, dialog);
//    }

    private void showDialog(Activity activity, Dialog dialog) {
        if (Build.VERSION.SDK_INT > 16) {
            if (!activity.isFinishing() || activity.isDestroyed()) {
                dialog.show();
            }
        } else {
            if (!activity.isFinishing()) {
                dialog.show();
            }
        }
    }

    public void dismissLoading() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
            dialog = null;
        }
    }

    /**
     * 关闭下拉刷新或上拉加载
     *
     * @param refreshLayout
     */
    protected void refrshOrLoadMoreFinished(RefreshLayout refreshLayout) {
        if (refreshLayout != null) {
            refreshLayout.finishRefresh();
            refreshLayout.finishLoadMore();
        }
    }


    public void handleEvent(BaseEvent event) {
        ToolLog.w("event", event.toString());
    }

    /**
     * 界面需要刷新时调用
     */
    public void refresh() {
    }

    protected void showToast(String str) {
        RxToast.showToastShort(str);
    }


}
